package com.mingte.manghe.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration //配置类
@EnableSwagger2// 开启Swagger2的自动配置
public class SwaggerConfig {

    /**
     * 通过 createRestApi函数来构建一个DocketBean
     * @ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
     * 访问--》   http://127.0.0.1:8080/swagger-ui.html
     */
    @Bean
    public Docket createRestApi(){
        List<Parameter> parameterList = new ArrayList<>();
        /*ParameterBuilder accessTokenBuilder = new ParameterBuilder();
        ParameterBuilder refreshTokenBuilder = new ParameterBuilder();
        accessTokenBuilder.name("authorization").description("程序员自测是动态传输的AccessToken入口")
                .modelRef(new ModelRef("String")).parameterType("header").required(false);
        refreshTokenBuilder.name("refreshToken").description("程序员自测是动态传输的RefreshToken入口")
                .modelRef(new ModelRef("String")).parameterType("header").required(false);
        parameterList.add(accessTokenBuilder.build());
        parameterList.add(refreshTokenBuilder.build());*/
        return new Docket(DocumentationType.SWAGGER_2)
                //调用apiInfo方法,创建一个ApiInfo实例,里面是展示在文档页面信息内容
                .apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.mingte.manghe.controller"))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(parameterList)
                .enable(true);
    }


    /**
     * 构建 api文档的详细信息函数
     * @return
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("XXX接口文档")
                //创建人
                .contact("XXX")
                //版本号
                .version("1.0")
                .termsOfServiceUrl("")
                //描述
                .description("XXX接口文档")
                .build();
    }
}